<?php

namespace app\controller\admin;

use core\controller\AdminBase;
use app\bll\AdminMenu as BllAdminMenu;
use app\bll\ComponentWidget as BllComponentWidget;
use app\bll\SiteRole as BllSiteRole;
use Exception;
use support\View;

class SiteRole extends AdminBase
{

    public function index()
    {

        $bllSiteRole = new BllSiteRole();
        $roles       = $bllSiteRole->paginator(
            title: request()->get('title', ''),
            siteid: request()->nowsiteid
        );

        View::assign('roles', $roles);
        return $this->view();
    }

    public function add()
    {

        if (is_post()) {
            try {
                $data           = request()->post();
                $data['siteid'] = request()->nowsiteid;
                $bllSiteRole    = new BllSiteRole();
                $bllSiteRole->add($data);
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {
            $bllAdminMenu = new BllAdminMenu();
            $menus = $bllAdminMenu->cascader(belong: 1);

            $bllComponentWidget = new BllComponentWidget();
            $widgets = $bllComponentWidget->select(1);

            View::assign('menus', $menus);
            View::assign('widgets', $widgets);

            return $this->view();
        }
    }

    public function edit()
    {
        $id = request()->get('id');
        $bllSiteRole    = new BllSiteRole();
        if (is_post()) {
            try {
                $data           = request()->post();
                $data['siteid'] = request()->nowsiteid;
                $bllSiteRole->edit($id, request()->nowsiteid, $data);
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {
            $bllAdminMenu = new BllAdminMenu();
            $menus = $bllAdminMenu->cascader(belong: 1);

            $bllComponentWidget = new BllComponentWidget();
            $widgets = $bllComponentWidget->select(1);

            $roleinfo = $bllSiteRole->find($id, request()->nowsiteid);
            $menuidents = [];
            if ($roleinfo['menus']) {
                $menuidents = array_column($roleinfo['menus'], 'ident');
            }

            $widgetidents = [];
            if ($roleinfo['widgets']) {
                $widgetidents = array_column($roleinfo['widgets'], 'ident');
            }

            View::assign('roleinfo', $roleinfo);
            View::assign('widgets', $widgets);
            View::assign('menus', $menus);
            View::assign('menuidents', $menuidents);
            View::assign('widgetidents', $widgetidents);
            return $this->view();
        }
    }


    public function delete()
    {

        try {
            $bllSiteRole    = new BllSiteRole();
            $bllSiteRole->delete(request()->get('id'), request()->nowsiteid);
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }
}
